using System.Collections;

namespace Is.Core
{
    internal class EnumerableEqualityComparer
    {
        public bool Equals(IEnumerable sourceList, IEnumerable targetList)
        {
            IEnumerator enumeratorSource = sourceList.GetEnumerator();
            IEnumerator enumeratorTarget = targetList.GetEnumerator();

            while (true)
            {
                bool hasNextX = enumeratorSource.MoveNext();
                bool hasNextY = enumeratorTarget.MoveNext();

                if (!hasNextX || !hasNextY)
                    return hasNextX == hasNextY;
                object currentSourceItem = enumeratorSource.Current;
                object currentTargetItem = enumeratorTarget.Current;
                if (Is.IsNullable(currentSourceItem) && currentSourceItem == null)
                {
                    if (Is.IsNullable(currentTargetItem) && currentTargetItem == null)
                    {
                        continue;
                    }
                    else
                    {
                        return false;
                    }
                }
                else if (Is.IsNullable(currentTargetItem) && currentTargetItem == null)
                {
                    return false;
                }

                if (currentSourceItem.Equals(currentTargetItem) == false)
                {
                    return false;
                }
                
            }
        }
    }
}